<?php

namespace app\sys\controller;

use app\common\model\PermissionModel;
use app\common\model\RoleModel;
use app\common\model\UserModel;
use think\Controller;
use think\Db;
use think\facade\Session;
use think\Request;

class Login extends Controller
{
    public function login(Request $request) {
        if (!$request->isAjax()) {
            return view();
        } else {
            $username = $request->param('username');
            $password = $request->param('password');
            $data['username'] = $username;
            $data['password'] = $password;
            $data['status'] = true;
            $data['msg'] = '登录成功';
            $user_model = model('common/UserModel');
            $users = $user_model->login($username, $password);
            if (count($users) < 1) {
                $data['status'] = false;
                $data['msg'] = '用户名或密码错误';
            } else if (count($users) > 1) {
                $data['status'] = false;
                $data['msg'] = '用户名和密码匹配有多个用户，请联系管理员解决......';
            } else {
                $role_model = model('common/RoleModel');
                $role = $role_model->where('id', $users[0]->role_id)->find();
                $users[0]['role_name'] = '暂无角色';
                if ($role != null) {
                    $users[0]['role_name'] = $role->name;
                }
                session('loginUser', $users[0]);
                session('loginUserName', $users[0]->name);
                session('loginUserId', $users[0]->id);
                $ps_model = model('common/PermissionModel');
                $data['permissions'] = $ps_model->get_login_user_permissions($users[0]->name);
                $data['user'] = $users[0];
            }
            return json($data);
        }
    }

    public function logout() {
        Session::clear();
        return redirect(url('/sys/login/login'));
    }


    public function my_info(Request $request) {
        if (!$request->isAjax()) {
            $user_session = session('loginUser');
            if ($user_session == null) {
                $this->redirect(url('/sys/login/login'));
            }
            $user = UserModel::where('id', '=', $user_session->id)->find();
            if ($user == null) {
                exit("<script type='text/javascript'>alert('对象不存在或已被删除');window.history.back(-1);</script>");
            }
            $user['role_name'] = $user_session->role_name;
            $data['user'] = $user;
            return view('', $data);
        } else {
            $id = $request->param('id');
            $user = UserModel::where('id', '=', $id)->find();
            if ($user == null) {
                return json([
                    'status' => false,
                    'msg' => '对象不存在或已被删除'
                ]);
            }
            $email = $request->param('email');
            $exits_users = UserModel::where('email', '=', $email)->whereNotIn('id', $id)->select();
            if (count($exits_users) > 0) {
                return json([
                    'status' => false,
                    'msg' => '邮箱不可用'
                ]);
            }

            $password = $request->param('password');
            if ($password != null && trim($password) != '') {
                $user['password'] = md5(trim($password));
            }

            $user['nick'] = $request->param('nick');
            $user['birthday'] = $request->param('birthday');
            $user['email'] = $email;
            $user['gender'] = $request->param('gender');
            $user['address'] = $request->param('address');
            $user['update_time'] = date('Y-m-d H:i:s', time());
            $user->save();
            $role = RoleModel::where('id', $user->role_id)->field('name')->find();
            $user['role_name'] = '暂无角色';
            if ($role != null) {
                $user['role_name'] = $role->name;
            }
            session('loginUser', $user);
            session('loginUserName', $user->name);
            session('loginUserId', $user->id);
            return json([
                'status' => true,
                'msg' => '修改成功'
            ]);
        }
    }

    public function test(){
        $menu_model = model('common/MenuModel');
        $menus = $menu_model->alias('m')->field('m.*,u.nick create_user')->join('sys_user u','m.user_id = u.id','left outer')->select();
        $data['menus'] = $menus;
        $data['sql']  =Db::getLastSql();//输出最后执行的SQL语句
        return json($data);
    }
}
